package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

/**
 *
 * @author Swee Zhi
 */


class WineVarietalManagement {
    
    def listWineVarietals() {
        def wineVarietals = WineVarietal.list()
        JSONArray arr = new JSONArray()
        wineVarietals.each() {
            JSONObject obj = new JSONObject()
            obj.put("id",it.id)
            obj.put("varietalName",it.varietalName)
            obj.put("description",it.description)
            arr.add(obj)
        }
        
        return arr
    }
    
    def getWineVarietalByID(id) {
        def varietalType = WineVarietal.findById(id)
        JSONObject obj = new JSONObject()
        obj.put("id",varietalType.id)
        obj.put("varietalName",varietalType.varietalName)
        obj.put("description",varietalType.description)
        
        return obj
    }
    
//    def getWineVarietalByName(name) {
//        def WineVarietal = WineVarietal.findByvarietalName(name)
//        JSONObject obj = new JSONObject()
//        obj.put("id",it.id)
//        obj.put("varietalName",it.varietalName)
//        obj.put("description",it.description)
//        
//        return obj
//    }
    
    def addWineVarietal(o) {
        def status
        
//        WineVarietal wv = new WineVarietal()
        def varietalName = o.get("varietalName")
        def description = o.get("description")
//        def wt = WineType.findByTypeName(o.get("wineType"))
//        wv.wineType = wt
        WineTypeManagement wtman = new WineTypeManagement()
        WineType wt = wtman.getWineTypeByTypeName(o.get("wineType"))
            
        wt.addToWineVarietal(new WineVarietal(varietalName:varietalName,description:description))
        
        if (wt.save()) {
            
//            println wt.id
//            wt.wineVarietal.add(wv)
            status = true
        }
        else {
            status = false
        }        
        println 'WineVarietalManagement >> Add Wine >> ' + status
        
        return status
    }
    
    def updateWineVarietal(wineVarietal) {
        def status
        
        WineVarietal wv = WineVarietal.findById(wineVarietal.id)
        if (wineVarietal.has("varietalName")) { wv.varietalName = wineVarietal.varietalName }
        if (wineVarietal.has("description")) { wv.description = wineVarietal.description }
        if (wineVarietal.has("wineType")) { wv.wineType = wineVarietal.wineType }
        if (wv.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'WineVarietalManagement >> Update Wine >> ' + status
        
        return status
    }
    
    def deleteWineVarietal(id) {
        def status
        
        WineVarietal wv = WineVarietal.findById(id)
        if (wv.delete()) {
            status = true
        }
        else {
            status = false
        }        
        println 'WineVarietalManagement >> Delete Wine >> ' + status
        
        return status
    }
    
}

